package com.centon.auto_code.project.service.impl;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.domain.ProModule;
import com.centon.auto_code.domain.gen.ProModuleCriteria;
import com.centon.auto_code.mapper.ProModuleMapper;
import com.centon.auto_code.project.service.ProModuleService;
import com.centon.auto_code.project.vo.ProModuleVo;
import com.centon.auto_code.util.BeanUtil;
import com.centon.auto_code.util.CommUtils;
import com.centon.auto_code.vo.BaseRes;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProModuleServiceImpl implements ProModuleService {

    @Autowired
    private ProModuleMapper proModuleMapper;


    @Override
    public BaseRes<List<ProModuleVo>> queryList(ProModuleVo proModuleVo) {



        BaseRes<List<ProModuleVo>> res = new BaseRes<>();
        ProModuleCriteria criteria = new ProModuleCriteria();
        criteria.createCriteria().andDelFlagEqualTo(0).andProjectIdEqualTo(proModuleVo.getProjectId());

        List<ProModule> proModules = proModuleMapper.selectByExample(criteria);

        if (proModules.size() > 0){
            List<ProModuleVo> proModuleVos = BeanUtil.voConvertList(proModules, ProModuleVo.class);
            res.setData(proModuleVos);
        }
        res.setServiceCode(ServiceCode.SUCCESS);

        return res;
    }

    @Override
    public BaseRes<String> addModule(ProModuleVo proModuleVo) {
        proModuleVo.setCreateTime(new Date());
        proModuleVo.setId(CommUtils.randomUUID());
        proModuleMapper.insertSelective(proModuleVo);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> updateModule(ProModuleVo proModuleVo) {
        proModuleVo.setUpdateTime(new Date());
        proModuleMapper.updateByPrimaryKeySelective(proModuleVo);
        return  new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> deleModule(ProModuleVo proModuleVo) {
        proModuleVo.setDelFlag(1);
        proModuleMapper.updateByPrimaryKeySelective(proModuleVo);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<ProModuleVo> moduleDetail(ProModuleVo proModuleVo) {

        BaseRes<ProModuleVo> res = new BaseRes<>();
        ProModule proModule = proModuleMapper.selectByPrimaryKey(proModuleVo.getId());

        if (proModule != null){
            ProModuleVo data = BeanUtil.voConvert(proModule, ProModuleVo.class);
            res.setData(data);
        }
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }
}
